var fileListDao = require("../dao/fileListDao");
var respUtil = require("../util/RespUtil");
var fs = require('fs');

var path = new Map();

function upload(request,response) {

    var name = request.file.originalname;
    var size = request.file.size;
    var path = request.file.path.replace(/\\/g,"/");

    console.log(path)
    var reg = new RegExp("[\\u4E00-\\u9FFF]+","g");

    if(reg.test(name)){
        fs.unlinkSync('./' + path);
        response.writeHead(500,respUtil.writeHeadChinese());
        response.write(respUtil.writeResult("error", "文件名不能有中文", null));
        response.end();
    }else {
        fileListDao.insertFileList(name,size,path, function (result) {
            console.log('图片写入数据库');
            response.end(path);
        });
    }
}

path.set("/upload",upload);

module.exports.path = path;